package ar.edu.unq.wannaplay.pages;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.components.WPLink;
import ar.edu.unq.wannaplay.dtos.EmployeeDTO;
import ar.edu.unq.wannaplay.pages.abm.employees.AddEmployeePage;
import ar.edu.unq.wannaplay.pages.abm.employees.EmployeeRegisterPage;
import ar.edu.unq.wannaplay.pages.abm.employees.ModifyEmployeePage;
import ar.edu.unq.wannaplay.services.EmployeeService;

public class EmployeePage extends WebPage {
    @SpringBean(name = "service.employeeService")
    private EmployeeService employeeService;

    public EmployeePage() {
        this.init();
    }

    private static final long serialVersionUID = 1L;

    public void init() {
        this.add(new PageableListView<EmployeeDTO>("tablaEmpleados", this.getEmployeeService().findAllDTOs(), 5) {

            private static final long serialVersionUID = 1L;
            PageParameters parameter;
            
            @Override
            protected void populateItem(final ListItem<EmployeeDTO> item) {
                String address = "address";
                final EmployeeDTO employee = item.getModelObject();
                item.add(new Label("name", employee.getName()));
                item.add(new Label(address, employee.getSurname()));
                item.add(new Label(address, employee.getDni()));
                item.add(new Label(address, employee.getAddress()));
                item.add(new Label("telephone", employee.getTelephone()));
                item.add(new Label("mail", employee.getEmail()));
                item.add(new Label(address, employee.getTown()));
                item.add(new Label(address, employee.getUniversity()));
                item.add(new Label(address, employee.getArt()));
                item.add(new Label(address, employee.getSocialPlan().getName()));
                
                parameter = new PageParameters();
                parameter.add("employee", String.valueOf(employee.getId()));
                item.add(new WPLink<ModifyEmployeePage>("edit", ModifyEmployeePage.class, parameter));
                
                item.add(new WPLink<EmployeeRegisterPage>("register", EmployeeRegisterPage.class, parameter));
                item.add(new Link<WebPage>("delete") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        EmployeePage.this.getEmployeeService().delete(employee);
                        EmployeePage.this.init();
                        // this.setResponsePage(EmpleadosPage.this);
                    }
                });
                this.add(new WPLink<AddEmployeePage>("add", AddEmployeePage.class));
            }
        });
    }

    // getters&setters
    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }
}
